<?php
/**
 * Created by PhpStorm.
 * User: patrickwu
 * Date: 2020/6/16
 * Time: 10:37
 */
namespace App\Http\Controllers\Live;

use App\Service\Live\AlterService;
use App\Service\Live\ClientService;
use Illuminate\Support\Facades\Redis;

class RoomController extends BaseController
{
    /**
     * client watch event handle
     * @param $websocket
     * @param $data
     */
    public function onWatch($websocket, $data) : void
    {
        $service    =   new ClientService();
        $user_id    =   $websocket->getUserId();
        $service->userWatch($this->current_room, $user_id);
    }

    /**
     * client focus on anchor event handle
     * @param $websocket
     * @param $data
     */
    public function onFocus($websocket, $data) : void
    {
        $service    =   new AlterService();
        $user_id    =   $websocket->getUserId();
        [$flag, $result]    =   $service->userFocus($user_id, $this->current_room);
        if($flag) {
            $websocket->emit('clientFocusResult', ['user_id' => $user_id, 'anchor_id' => $result['anchor_id'] ?? '']);
            $websocket->to($this->current_room)->emit('clientWelcome', ['type' => self::FOCUS_WELCOME, 'msg' => sprintf(self::WELCOME_DESC[self::FOCUS_WELCOME], ($user_info['nick_name'] ?? ''))]);
        }
    }

    /**
     * client cancel focus
     * @param $websocket
     * @param $data
     */
    public function onCancelFocus($websocket, $data) : void
    {
        $service    =   new AlterService();
        $user_id    =   $websocket->getUserId();
        [$flag, $result]    =   $service->cancelFocus($user_id, $this->room_id);
        if($flag) {
            $websocket->emit('clientCancelResult', ['user_id' => $user_id, 'anchor_id' => $result['anchor_id'] ?? '']);
//            $websocket->to($this->current_room)->emit('clientWelcome', ['type' => self::COME_WELCOME, 'msg' => sprintf(self::WELCOME_DESC[self::COME_WELCOME], ($user_info['nick_name'] ?? ''))]);
        }
    }

}
